package com.isoftstone.userinfo.service.impl;

import com.isoftstone.platform.entity.PadingRstType;
import com.isoftstone.platform.entity.PagingBean;
import com.isoftstone.platform.model.entity.PtUserEntity;
import com.isoftstone.platform.tag.entity.SelectTagEntity;
import com.isoftstone.platform.tag.service.DataSelectService;
import com.isoftstone.userinfo.entity.*;
import com.isoftstone.userinfo.repository.UserInfoDao;
import com.isoftstone.userinfo.service.UserInfoServicee;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

@Service("userInfoService2")
public class UserInfoServiceImple implements UserInfoServicee, DataSelectService {
    @Resource
    private UserInfoDao userInfoDao2;
    @Override
    public List<UserInfoTreeEntity> queryUserInfoZtreeList(@RequestParam("userUuid") String userUuid) {
        List<UserOrgan> userOrgans = userInfoDao2.queryUserOrganByUseruuid(userUuid);
        List<UserInfoTreeEntity> userInfoTreeEntities =  userInfoDao2.queryUserInfoZtreeList(userUuid);
        if(userOrgans.size()>0){
            for (UserInfoTreeEntity userInfoTreeEntity : userInfoTreeEntities) {
                for (UserOrgan userOrgan : userOrgans) {
                    if(userOrgan.getDutyid().equals(userInfoTreeEntity.getId())){
                        userInfoTreeEntity.setChecked(true);
                    }
                }
            }
        }
        return userInfoTreeEntities;
    }


    @Override
    public void updateUserDail(PtUserEntity ptUserEntity) {
        userInfoDao2.updateUserDail(ptUserEntity);
    }

    @Override
    public void addUserInfo(PtUserEntity ptUserEntity) {
        userInfoDao2.addUserInfo(ptUserEntity);
    }

    @Override
    public List<RoleTreeEntity> queryRoleInfoZtreeList(@RequestParam("userUuid") String userUuid) {
        List<UserRole> userRoles = userInfoDao2.queryUserRoleByUseruuid(userUuid);
        List<RoleTreeEntity> roleTreeEntities =  userInfoDao2.queryRoleInfoZtreeList(userUuid);
        if(userRoles.size()>0){
            for (RoleTreeEntity roleTreeEntity : roleTreeEntities) {
                for (UserRole userRole : userRoles) {
                    if(userRole.getRoleUuid().equals(roleTreeEntity.getId())){
                        roleTreeEntity.setChecked(true);
                    }
                }
            }
        }
        return roleTreeEntities;

    }

    @Override
    public PadingRstType<PtUserEntity> getUserInfolist(SearchUserInfoEntity search, PagingBean pagingBean) {
        pagingBean.deal(PtUserEntity.class);
        PadingRstType<PtUserEntity> padingRstType = new PadingRstType<PtUserEntity>();
        padingRstType.setPage(pagingBean.getPage());
        List<PtUserEntity> list = userInfoDao2.getUserInfolistByPage(search,pagingBean);
        padingRstType.setRawRecords(list);
        Integer total = userInfoDao2.getUserInfolistTotal(search);
        padingRstType.setTotal(total);
        padingRstType.putItems(PtUserEntity.class,list);
        return padingRstType;
    }

    @Override
    public List<PtUserEntity> queryUserInfoById(String id) {
        return userInfoDao2.queryUserInfoById();
    }

    @Override
    public void bondRoleUser(String[] roleUuid, String userUuid) {

        for (String s : roleUuid) {
            userInfoDao2.bondRoleUser(s,userUuid);
        }
    }

    @Override
    public void cancelRoleBound(String userUuid) {
        userInfoDao2.cancelRoleBound(userUuid);
    }

    @Override
    public void deleteUserInfo(String ids) {
        String[] idArray = ids.split(",");
        userInfoDao2.deleteUserInfo(idArray);
    }


    @Override
    public List<SelectTagEntity> queryTagElementList(String language) {
        return userInfoDao2.queryTagElementList();
    }
}
